#!/bin/bash

# Java compile wrapper-script for 'compile.sh'.
# See that script for syntax and more info.
#
# This script byte-compiles with the kotlinc compiler and
# generates a shell script to run it with the java interpreter with Kotlin
# libraries later.
#
# This script requires that kotlinc is installed in the chroot.

DEST="$1" ; shift
MAINSOURCE="$1"
MAINCLASS=""
COMPILESCRIPTDIR="$(dirname "$0")"

# Allow overriding Kotlin main class (should not be necessary):
if [ -z "$ENTRY_POINT" ]; then
	[ -n "$DEBUG" ] && echo "Debug: no ENTRY_POINT provided, trying to detect main class."
else
	[ -n "$DEBUG" ] && echo "Debug: using main class provided by ENTRY_POINT='$ENTRY_POINT'."
	MAINCLASS="$ENTRY_POINT"
fi

# Byte-compile:
kotlinc -d . "$@"
EXITCODE=$?
[ "$EXITCODE" -ne 0 ] && exit $EXITCODE

if [ -z "$MAINCLASS" ]; then
	# Look for class that has the 'main' function:
	CLASSNAMES="$(find ./* -type f -regex '^.*\.class$' \
	            | sed -e 's/\.class$//' -e 's/^\.\///' -e 's/\//./g')"
	MAINCLASS=$(java -cp "$COMPILESCRIPTDIR" DetectMain "$(pwd)" $CLASSNAMES)
	EXITCODE=$?

	# Report the entry point, so it can be saved, e.g. for later replay:
	echo "Info: detected entry_point: $MAINCLASS"

	[ "$EXITCODE" -ne 0 ] && exit $EXITCODE
fi

# Check if entry point is valid
javap "$MAINCLASS" > /dev/null
EXITCODE=$?
[ "$EXITCODE" -ne 0 ] && exit $EXITCODE

# Write executing script:
# Executes java byte-code interpreter with following options (prefixed
# with -J to pass from kotlin to java):
# -Xmx: maximum size of memory allocation pool
# -Xms: initial size of memory, improves runtime stability
# -XX:+UseSerialGC: Serialized garbage collector improves runtime stability
# -Xss${MEMSTACK}k: stack size as configured abve
# -Dfile.encoding=UTF-8: set file encoding to UTF-8
cat > "$DEST" <<EOF
#!/bin/sh
# Generated shell-script to execute java interpreter on source.
# Detect dirname and change dir to prevent class not found errors.
if [ "\${0%/*}" != "\$0" ]; then
	cd "\${0%/*}"
fi

# Stack size in the JVM in KB. Note that this will be deducted from
# the total memory made available for the heap.
MEMSTACK=65536

# Amount of memory reserved for the Java virtual machine in KB. The
# default below is just above the maximum memory usage of current
# versions of the jvm, but might need increasing in some cases.
MEMJVM=65536

MEMRESERVED=\$((MEMSTACK + MEMJVM))

# Calculate Java program memlimit as MEMLIMIT - max. JVM memory usage:
MEMLIMITJAVA=\$((MEMLIMIT - MEMRESERVED))

if [ \$MEMLIMITJAVA -le 0 ]; then
	echo "internal-error: total memory \$MEMLIMIT KiB <= \$MEMJVM + \$MEMSTACK = \$MEMRESERVED KiB reserved for JVM and stack leaves none for heap."
	exit 1
fi

if [ -z "\$MEMLIMIT" ]; then
	exec kotlin -J-DONLINE_JUDGE -Dfile.encoding=UTF-8 -J-XX:+UseSerialGC -J-Xss\${MEMSTACK}k '$MAINCLASS' "\$@"
else
	exec kotlin -J-DONLINE_JUDGE -Dfile.encoding=UTF-8 -J-XX:+UseSerialGC -J-Xss\${MEMSTACK}k -J-Xms\${MEMLIMITJAVA}k -J-Xmx\${MEMLIMITJAVA}k '$MAINCLASS' "\$@"
fi
EOF

chmod a+x "$DEST"

exit 0